﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using 开单.Dao.基础;
using System.IO;
using 开单.ComMethod;

namespace 开单
{
    public partial class FrmXsdInfo : Form
    {
        XsdDao xsdDao = new XsdDao();
        XsdEntity xsdEntity = new XsdEntity();
        KhxxDao khxxDao = new KhxxDao();
        YgxxDao ygxxDao = new YgxxDao();
        XsmxDao xsmxDao = new XsmxDao();
        XsmxEntity xsmxEntity = new XsmxEntity();
        DataTable dt = new DataTable();
        DataTable dt3 = new DataTable();
        DataTable dt2 = new DataTable();
        string state = null;
        public delegate bool AsyncEventHandler();
        private static FrmXsdInfo Instance;
        private FrmXsdInfo()
        {
            InitializeComponent();
            uClistMoreKh.ListEvent += new ClientApplication.ComControl.UClistMore.ListEventHandler(uClistMoreKh_ListEvent);
            uClistMoreKdr.ListEvent += new ClientApplication.ComControl.UClistMore.ListEventHandler(uClistMoreKdr_ListEvent);
            uClistMoreShr.ListEvent += new ClientApplication.ComControl.UClistMore.ListEventHandler(uClistMoreShr_ListEvent);
        }

        void uClistMoreShr_ListEvent(开单.ComMethod.MyListItem myList)
        {
            TxtShr.Text = myList.TextField;
            TxtShr.Tag = myList.ValueField;
            uClistMoreShr.Visible = false;
            TxtShr.Focus();
        }

        void uClistMoreKdr_ListEvent(开单.ComMethod.MyListItem myList)
        {
            TxtKdr.Text = myList.TextField;
            TxtKdr.Tag = myList.ValueField;
            uClistMoreKdr.Visible = false;
            TxtKdr.Focus();
        }

        void uClistMoreKh_ListEvent(开单.ComMethod.MyListItem myList)
        {
            TxtKh.Text = myList.TextField;
            TxtKh.Tag = myList.ValueField;
            uClistMoreKh.Visible = false;
            TxtKh.Focus();
        }
        public static FrmXsdInfo GetInstance()
        {
            Instance = new FrmXsdInfo();
            return Instance;
        }
        private void toolStripQuery_Click(object sender, EventArgs e)
        {
            string sTime = DataUtil.FormatToStandard(DtpStime.Value.ToShortDateString());
            string eTime = DataUtil.FormatToStandard(DtpEtime.Value.Add(System.TimeSpan.FromDays(1)).ToShortDateString());
            dt= xsdDao.GetXsdDt(TxtKh.Text.Trim(),TxtKdr.Text.Trim(),TxtShr.Text.Trim(), sTime, eTime);
            dataGridView1.DataSource = dt;
            //if (dataGridView1.RowCount != 0) //暂时
            //{
            //    toolStripExport.Visible = true;
            //}
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if(dataGridView1.RowCount==0){return;}
            if(dataGridView1.CurrentCell.RowIndex==-1){return;}
            dt=xsmxDao.GetXsmxDt(dataGridView1.CurrentRow.Cells[0].Value.ToString());
            dataGridView2.DataSource = dt;
            dataGridView2.Columns[0].Visible = false;

        }

        private void toolStripClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void TxtKh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (0 != dt.Rows.Count)
                {
                    if (dt.Rows[0][1].ToString() == TxtKh.Text) { return; }
                }
                dt = khxxDao.GetKhxxDt(TxtKh.Text.Trim());
                uClistMoreKh.addData(dt);
            }
        }

        private void toolStripDel_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count == 0) { MessageBox.Show("没有可删除的记录"); return; }
            if (!(MessageBox.Show("确定要删除选中的销售单及其明细吗？", "提示", MessageBoxButtons.YesNo,
                      MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)) { return; }
            AsyncEventHandler asy = DelManager;
            //异步调用开始，没有回调函数和AsyncState,都为null 
            IAsyncResult ia = asy.BeginInvoke(MethodCompleted, asy);
            pictureBox1.Visible = true;
            label1.Visible = true;
            toolStrip1.Enabled = false;
            state = "deling";
        }

        private void MethodCompleted(IAsyncResult asyncResult)
        {
            pictureBox1.Visible = false;
            label1.Visible = false;
            toolStrip1.Enabled = true;
            dataGridView2.DataSource = null;
            toolStripQuery_Click(null, null);
            MessageBox.Show("已成功删除");
            state = null;
        }

        private bool DelManager()
        {
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                if (dataGridView1.Rows[i].Selected==true)
                {
                    xsdDao.DelxsxxByXsdcode(dataGridView1[0, i].Value.ToString());
                    xsmxDao.DelxsmxByXsdcode(dataGridView1[0, i].Value.ToString());
                }
            }
            return true; 
        }

        private void TxtKdr_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (0 != dt2.Rows.Count)
                {
                    if (dt2.Rows[0][1].ToString() == TxtKdr.Text) { return; }
                }
                dt2 = ygxxDao.GetYgxxDt(TxtKdr.Text.Trim());
                uClistMoreKdr.addData(dt2);
            }
        }

        private void TxtShr_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (0 != dt3.Rows.Count)
                {
                    if (dt3.Rows[0][1].ToString() == TxtShr.Text) { return; }
                }
                dt3 = ygxxDao.GetYgxxDt(TxtShr.Text.Trim());
                uClistMoreShr.addData(dt3);
            }
        }

        private void toolStripClearAll_Click(object sender, EventArgs e)
        {
            IntityControl();
        }

        private void IntityControl()
        {
            TxtKh.Text = "";
            TxtKh.Tag = null;
            TxtKdr.Text = "";
            TxtKdr.Tag = null;
            TxtShr.Text = "";
            TxtShr.Tag = null;
            dataGridView1.DataSource = null;
            dataGridView2.DataSource = null;
            TxtKh.Focus();
        }

        private void FrmXsdInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (state == "deling"||state=="expoing")
            {
                e.Cancel = true;
            }
        }

        private void toolStripExport_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count == 0) { MessageBox.Show("没有可删除的记录"); return; }
            if (!(MessageBox.Show("确定要导出列表中所有的销售单及其明细吗？", "提示", MessageBoxButtons.YesNo,
            MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)) { return; }
            string[] str = new string[dataGridView1.Rows.Count];
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                str[i] = dataGridView1[0, i].Value.ToString();
            }
            DataTable dtForExport = xsdDao.getInfoToExport(str);
            //ExportToExcel.MangerExport(dtForExport);
            //ExportToExcel.MethedEndEvent += new ExportToExcel.MethedEndEventHandler(ExportToExcel_MethedEndEvent);
            exportToExcel1.MangerExport(dtForExport);
            exportToExcel1.MethedEndEvent += new 开单.ComControl.ExportToExcel.MethedEndEventHandler(exportToExcel1_MethedEndEvent);
            //pictureBox1.Visible = true;
            label4.Visible = true;
            toolStrip1.Enabled = false;
            state = "expoing";
        }

        void exportToExcel1_MethedEndEvent()
        {
            //pictureBox1.Visible = false;
            label4.Visible = false;
            toolStrip1.Enabled = true;
            dataGridView2.DataSource = null;
            toolStripQuery_Click(null, null);
            state = null;
        }

        void ExportToExcel_MethedEndEvent()
        {
            pictureBox1.Visible = false;
            label4.Visible = false;
            toolStrip1.Enabled = true;
            dataGridView2.DataSource = null;
            toolStripQuery_Click(null, null);
            state = null;
        }
    }
}
